home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / game / wins1726.zip / GIFVIEW.C < prev    next >
Text File  |  1992-01-03  |  6KB  |  251 lines

  1. /*
  2.  *
  3.  * This GIF decoder is designed for use with Bert Tyler's FRACTINT
  4.  * program. It should be noted that the "FRACTINT" program only decodes
  5.  * GIF files FRACTINT creates, so this decoder code lacks full generality
  6.  * in the following respects: supports single image, non-interlaced GIF
  7.  * files with no local color maps and no extension blocks.
  8.  *
  9.  * GIF and 'Graphics Interchange Format' are trademarks (tm) of
  10.  * Compuserve, Incorporated, an H&R Block Company.
  11.  *
  12.  *                                            Tim Wegner
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <dos.h>
  18. #include "fractint.h"
  19.  
  20. /* routines in this module    */
  21.  
  22. int  gifview(void);
  23. int  get_byte(void);        /* used only locally and by decoder.c */
  24. int  get_bytes(char *,int);    /* used only by decoder.c  *mg*/
  25.  
  26. static void close_file(void);
  27.  
  28. #define MAXCOLORS    256
  29.  
  30. extern int timer(int timertype,int(*subrtn)(),...);
  31. extern int rowcount;        /* row counter for screen */
  32. extern char readname[];     /* file name          */
  33. static FILE *fpin = NULL;    /* FILE pointer       */
  34. unsigned int height;
  35. extern    char busy;
  36. unsigned numcolors;
  37.  
  38. extern char MAP_name[];
  39. extern int mapset;
  40. extern int colorstate;        /* comments in cmdfiles */
  41.  
  42. extern int glassestype;
  43. extern int display3d;
  44. extern int dotmode;        /* so we can detect disk-video */
  45. extern int calc_status;
  46. extern long calctime;
  47. extern long timer_interval;
  48. extern int pot16bit;        /* 16 bit values for continuous potential */
  49.  
  50. int bad_code_count = 0;     /* needed by decoder module */
  51.  
  52. int get_byte()
  53. {
  54.    return (getc(fpin)); /* EOF is -1, as desired */
  55. }
  56.  
  57. int get_bytes(char *where,int how_many)
  58. {
  59.    return (fread(where,1,how_many,fpin)); /* EOF is -1, as desired */
  60. }
  61.  
  62. extern unsigned char dacbox[256][3];    /* Video-DAC (filled in by SETVIDEO) */
  63. extern unsigned char decoderline[2049]; /* write-line routines use this */
  64.  
  65. /* Main entry decoder */
  66. int gifview()
  67. {
  68.    unsigned char buffer[16];
  69.    unsigned width, finished;
  70.    char temp1[81];
  71.  
  72.    int status;
  73.    int i, j, k, planes;
  74.  
  75.    status = 0;
  76.  
  77.    /* initialize the row count for write-lines */
  78.    rowcount = 0;
  79.  
  80.    /* zero out the full write-line */
  81.    for (width = 0; width < 2049; width++) decoderline[width] = 0;
  82.  
  83.    /* Open the file */
  84.    strcpy(temp1,readname);
  85.    if (strchr(temp1,'.') == NULL) {
  86.       strcat(temp1,DEFAULTFRACTALTYPE);
  87.       if ((fpin = fopen(temp1,"rb")) != NULL) {
  88.      fclose(fpin);
  89.      }
  90.       else {
  91.      strcpy(temp1,readname);
  92.      strcat(temp1,ALTERNATEFRACTALTYPE);
  93.      }
  94.       }
  95.    if ((fpin = fopen(temp1, "rb")) == NULL)
  96.       return (-1);
  97.  
  98.    /* Get the screen description */
  99.    for (i = 0; i < 13; i++)
  100.    {
  101.       int tmp;
  102.  
  103.       buffer[i] = tmp = get_byte();
  104.       if (tmp < 0)
  105.       {
  106.      close_file();
  107.      return(-1);
  108.       }
  109.    }
  110.  
  111.    if(strncmp(buffer,"GIF87a",3) ||             /* use updated GIF specs */
  112.       buffer[3] < '0' || buffer[3] > '9' ||
  113.       buffer[4] < '0' || buffer[4] > '9' ||
  114.       buffer[5] < 'A' || buffer[5] > 'z' )
  115.    {
  116.       close_file();
  117.       return(-1);
  118.    }
  119.  
  120.    planes = (buffer[10] & 0x0F) + 1;
  121.  
  122.    if((buffer[10] & 0x80)==0)     /* color map (better be!) */
  123.    {
  124.       close_file();
  125.       return(-1);
  126.    }
  127.    numcolors = 1 << planes;
  128.  
  129.    for (i = 0; i < numcolors; i++)
  130.    {
  131.       for (j = 0; j < 3; j++) {
  132.      if ((k = get_byte()) < 0)
  133.      {
  134.         close_file();
  135.         return(-1);
  136.      }
  137.      if(!display3d || (glassestype != 1 && glassestype != 2))
  138.         dacbox[i][j] = k >> 2;
  139.       }
  140.    }
  141.    colorstate = 1; /* colors aren't default and not a known .map file */
  142.  
  143.    /* don't read if glasses */
  144.    if (display3d && mapset && glassestype!=1 && glassestype != 2)
  145.    {
  146.        ValidateLuts(MAP_name);    /* read the palette file */
  147.        spindac(0,1); /* load it, but don't spin */
  148.    }
  149.    if (dacbox[0][0] != 255)
  150.       spindac(0,1);      /* update the DAC */
  151.  
  152.    if (dotmode == 11) /* disk-video */
  153.        dvid_status(1,"...restoring...");
  154.  
  155.    /* Now display one or more GIF objects */
  156.    finished = 0;
  157.    while (!finished)
  158.    {
  159.       switch (get_byte())
  160.       {
  161.       case ';':
  162.      /* End of the GIF dataset */
  163.  
  164.      finished = 1;
  165.      status = 0;
  166.      break;
  167.  
  168.       case '!':                               /* GIF Extension Block */
  169.      get_byte();             /* read (and ignore) the ID */
  170.      while ((i = get_byte()) > 0)     /* get the data length */
  171.         for (j = 0; j < i; j++)
  172.            get_byte();     /* flush the data */
  173.      break;
  174.       case ',':
  175.      /*
  176.       * Start of an image object. Read the image description.
  177.       */
  178.  
  179.      for (i = 0; i < 9; i++)
  180.      {
  181.             int tmp;
  182.  
  183.             buffer[i] = tmp = get_byte();
  184.         if (tmp < 0)
  185.         {
  186.            status = -1;
  187.            break;
  188.         }
  189.      }
  190.      if(status < 0)
  191.      {
  192.         finished = 1;
  193.         break;
  194.      }
  195.  
  196.      width    = buffer[4] | (buffer[5] << 8);
  197.      if (pot16bit) width >>= 1;
  198.      height = buffer[6] | (buffer[7] << 8);
  199.  
  200.          /* Skip local color palette */
  201.          if((buffer[8] & 0x80)==0x80) {      /* local map? */
  202.              int numcolors;    /* make this local */
  203.              planes = (buffer[8] & 0x0F) + 1;
  204.              numcolors = 1 << planes;
  205.              /* skip local map */
  206.              for (i = 0; i < numcolors; i++) {
  207.                 for (j = 0; j < 3; j++) {
  208.                    if ((k = get_byte()) < 0) {
  209.                       close_file();
  210.                       return(-1);
  211.                       }
  212.                    }
  213.                 }
  214.              }
  215.  
  216.      if (calc_status == 1) /* should never be so, but make sure */
  217.         calc_status = 0;
  218.      busy = 1;    /* for slideshow CALCWAIT */
  219.      status = timer(1,NULL,width); /* call decoder(width) via timer */
  220.      busy = 0;    /* for slideshow CALCWAIT */
  221.      if (calc_status == 1) /* e.g., set by line3d */
  222.      {
  223.         calctime = timer_interval; /* note how long it took */
  224.         if (keypressed() != 0)
  225.            calc_status = 3; /* interrupted, not resumable */
  226.         else
  227.            calc_status = 4; /* complete */
  228.      }
  229.      finished = 1;
  230.      break;
  231.       default:
  232.      status = -1;
  233.      finished = 1;
  234.      break;
  235.       }
  236.    }
  237.    close_file();
  238.    if (dotmode == 11) { /* disk-video */
  239.       dvid_status(0,"Restore completed");
  240.       dvid_status(1,"");
  241.       }
  242.  
  243.    return(status);
  244. }
  245.  
  246. static void close_file()
  247. {
  248.    fclose(fpin);
  249.    fpin = NULL;
  250. }
  251.